//
//  SXHomeViewController.m
//  NetEase-News
//
//  Created by 沈欣 on 2017/9/12.
//  Copyright © 2017年 沈欣. All rights reserved.
//

#import "SXHomeViewController.h"
#import "SXChannelModel.h"
#import "SXChannelLabel.h"
#import "SXHomeCollectionViewCell.h"

@interface SXHomeViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *channelScrollView;
@property (weak, nonatomic) IBOutlet UICollectionView *homeCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *homeFlowLayout;

@end

@implementation SXHomeViewController {
    ///频道模型数组
    NSArray *_channels;
    NSMutableArray *_channelsLabels;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //禁止滚动视图向下偏移64个像素
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    [self createChannelLabels];
    
    [self setupHomeCollectionView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    
    //设置cell大小
    self.homeFlowLayout.itemSize = self.homeCollectionView.bounds.size;
}

- (void)createChannelLabels {
    _channels = [SXChannelModel getChannelModels];
    NSLog(@"%@",_channels);
    
    CGFloat labelW = 80;
    CGFloat labelH = 44;
    
    //遍历频道标签模型数组,创建对应的视频标签并赋值
    for (int i = 0; i < _channels.count; i++) {
        SXChannelLabel *label = [SXChannelLabel new];
        [self.channelScrollView addSubview:label];
        
        //设置frame
        CGFloat labelX = labelW * i;
        label.frame = CGRectMake(labelX, 0, labelW, labelH);
        
        //赋值
        SXChannelModel *model = _channels[i];
        label.text = model.tname;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(channelLabelTapClick:)];
        [label addGestureRecognizer:tap];
        label.userInteractionEnabled = YES;
        label.tag = i;
        [_channelsLabels addObject:label];
        
        if (i == 0) {
            label.scale = 1.0;
        }
    }
    //设置滚动范围
    self.channelScrollView.contentSize = CGSizeMake(labelW * _channels.count, 0);
    self.channelScrollView.bounces = NO;
    self.channelScrollView.showsHorizontalScrollIndicator = NO;
}

///设置homeCollectionView
- (void)setupHomeCollectionView {
    self.homeCollectionView.dataSource = self;
    self.homeCollectionView.delegate = self;
    self.homeCollectionView.pagingEnabled = YES;
    self.homeCollectionView.bounces = NO;
    
    self.homeFlowLayout.minimumLineSpacing = 0;
    self.homeFlowLayout.minimumInteritemSpacing = 0;
    
    self.homeFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}

#pragma mark
#pragma mark - 频道点击 == 频道标签联动新闻滚动视图
- (void)channelLabelTapClick:(UITapGestureRecognizer *)tap {
    SXChannelLabel *tapLabel = (SXChannelLabel *)tap.view;
    CGFloat offsetX = tapLabel.center.x - (self.view.bounds.size.width * 0.5);
    CGFloat minX = 0;
    CGFloat maxX = self.channelScrollView.contentSize.width - self.view.bounds.size.width;
    if (offsetX < minX) {
        offsetX = minX;
    }
    if (offsetX > maxX) {
        offsetX = maxX;
    }
    CGPoint point = CGPointMake(offsetX, 0);
    
    [self.channelScrollView setContentOffset:point animated:YES];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:tapLabel.tag inSection:0];
    [self.homeCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    
    for (int i = 0; i < _channelsLabels.count; i++) {
        //遍历每个标签
        SXChannelLabel *label = _channelsLabels[i];
        if (i == tapLabel.tag) {
            label.scale = 1.0;
        } else {
            label.scale = 0.0;
        }
    }
}

#pragma mark
#pragma mark - UICollectionViewDelegate
/// 监听UICollectionView实时滚动 == 频道标签缩放
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / self.view.bounds.size.width;
    CGFloat index_float = scrollView.contentOffset.x / self.view.bounds.size.width;
    CGFloat percent = index_float - index;
    NSLog(@"%f - %zd = %f",index_float,index,percent);
    // 右边的缩放比
    CGFloat rightScale = percent;
    // 左边的缩放比
    CGFloat leftScale = 1 - rightScale;
    // 左边标签索引
    NSInteger leftIndex = index;
    // 右边标签索引
    NSInteger rightIndex = 1 + leftIndex;
    
    //    NSLog(@"左%zd=%f - 右%zd=%f",leftIndex,leftScale,rightIndex,rightScale);
    
    // 获取左边频道标签
    SXChannelLabel *leftLabel = _channelsLabels[leftIndex];
    leftLabel.scale = leftScale;
    
    // 获取右边频道标签 : 防止角标越界
    if (rightIndex < _channelsLabels.count) {
        SXChannelLabel *rightLabel = _channelsLabels[rightIndex];
        rightLabel.scale = rightScale;
    }
}

/// 监听UICollectionView滚动停止 == 新闻滚动视图联动频道标签
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 1.把滚动到的新闻视图对应的频道标签居中
    // 1.1 获取新闻视图滚动到的索引
    NSInteger index = scrollView.contentOffset.x / self.view.bounds.size.width;
    // 1.2 找到滚动到的新闻视图对应的频道标签
    SXChannelLabel *label = _channelsLabels[index];
    // 1.3 计算滚动距离
    CGFloat offsetX = label.center.x - (self.view.bounds.size.width*0.5);
    
    // 1.4 设置临界范围
    CGFloat minX = 0;
    CGFloat maxX = self.channelScrollView.contentSize.width - self.view.bounds.size.width;
    if (offsetX < minX) {
        offsetX = minX;
    }
    if (offsetX > maxX) {
        offsetX = maxX;
    }
    
    // 1.5 确定需要滚动到的点
    CGPoint point = CGPointMake(offsetX, 0);
    // 1.6 把滚动到的新闻视图对应的频道标签居中
    [self.channelScrollView setContentOffset:point animated:YES];
}

#pragma mark
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _channels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SXHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SXHomeCollectionViewCell" forIndexPath:indexPath];
    //获取新闻界面对应的tid
    SXChannelModel *model = _channels[indexPath.item];
    //拼接新闻数据对应的URL
    NSString *URLString = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/%@/0-20.html",model.tid];
    //把新闻数据地址传入到cell
    cell.URLString = URLString;
    
    return cell;
}


@end
